<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24" class="top">
        <el-form :model="searchParams" ref="searchParams" :inline="true">
          <el-form-item label="所属商品ID">
            <el-input v-model="searchParams.item" maxlength='20' placeholder="请输入" clearable
              show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="所属商品分类" prop="cate">
            <el-select v-model="searchParams.itemCate" placeholder="请选择" clearable>
              <el-option v-for="item in cateList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="功能分类：" prop="activity">
            <el-select v-model="searchParams.itemActivity" placeholder="请选择" clearable>
              <el-option v-for="item in activityList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格值">
            <el-input v-model="searchParams.specValue" maxlength='20' placeholder="请输入" clearable
              show-word-limit></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSearch" type="primary" icon="el-icon-search"
              style="margin-left:40px">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-form :model="searchParams" ref="searchParams" :inline="true" style="margin:10px 0">
          <!-- <el-button @click="operateAction('create', {})" type="primary" plain size='small'>新增</el-button>
          <el-button @click="operateAction('delete', {})" type="info" plain size='small'>删除</el-button> -->
        </el-form>
        
        <el-collapse>
          <el-collapse-item title="批量修改" name="1">
            <el-form :inline="true">
              <el-form-item label="" prop="">
                <div style="display: flex;flex-direction: row;align-items: center;">
                      <div style="font-size: 14px;margin-left: 10px;">库存</div>
                      <el-input style="margin-left: 10px;width: 120px;" v-model="setSkuData.stock" type="number"></el-input>
                      <div style="margin-left: 10px;"><el-button @click="updateAllStock" type="primary" size="small">确认修改</el-button></div>
                      <div style="font-size: 14px;margin-left: 10px;">销售价</div>
                      <el-input style="margin-left: 10px;width: 120px;" v-model="setSkuData.sellingPrice" type="number"></el-input>
                      <div style="margin-left: 10px;"><el-button @click="updateAllSellingPrice" type="primary" size="small">确认修改</el-button></div>
                  </div>
              </el-form-item>
            </el-form>
          </el-collapse-item>
        </el-collapse>

        <el-table :data="dataList" size='medium' ref="multipleTable" @selection-change="handleSelectionChange" border>
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="ID" width="50" show-overflow-tooltip></el-table-column>
          <el-table-column prop="item" label="所属商品信息" show-overflow-tooltip width="160">
            <template slot-scope="{ row, index }">
              <div>商品ID：{{ row.item }}</div>
              <div>商品名称：{{ row.itemModel.name }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="item" label="商品分类" show-overflow-tooltip width="160">
            <template slot-scope="{ row, index }">
              <div>商品分类：{{ row.itemModel.cateName }}</div>
              <div>功能分类：{{ row.itemModel.activityName }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="coverUrl" label="封面图" show-overflow-tooltip>
            <template slot-scope="{row, index}"><img width="50" height="50" :src="row.coverUrl"></template>
          </el-table-column>
          <el-table-column prop="specValues" label="规格字符串" show-overflow-tooltip width="280"></el-table-column>
          <el-table-column prop="sellingPrice" label="销售价" show-overflow-tooltip></el-table-column>
          <el-table-column prop="sales" label="销量" show-overflow-tooltip></el-table-column>
          <el-table-column prop="stock" label="库存" show-overflow-tooltip></el-table-column>
          
          <el-table-column prop="redact" label="操作" fixed="right" width="120">>
            <template slot-scope="{ row, index }">
              <!-- <el-button type="text" @click="operateAction('update', row)" size='medium'>修改</el-button>
              <el-button type="text" @click="operateAction('detail', row)" size='medium'>详情</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <pagination style="background: #ffffff; height: 50px" v-show="totalSize > 0" :total="totalSize"
          :page.sync="page" :limit.sync="size" @pagination="getDataList" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { formData } from './data'
import { findSkuList, updateAllStock, updateAllSellingPrice } from '@/api/merchant/sku.js'
import { findCateList } from '@/api/merchant/cate.js'
import { findActivityList } from '@/api/merchant/activity.js'
export default {
  components: { },
  data() {
    return {
      page: 1,
      size: 10,
      totalSize: 0,
      dataList: [],
      searchParams: {},
      multipleSelection: [],
      formData,
      cateList: [],
      activityList: [],
      setSkuData: {}
      
    }
  },
  created() {
    this.getDataList()
    this.findCateList()
    this.findActivityList()
  },
  methods: {
    handleSearch() {
      this.page = 1
      this.getDataList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    operateAction(scene, row) {
      switch (scene) {
        case 'create':
          this.$router.push({name: 'Update', query: {formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name }})
          break;
        case 'update':
          this.$router.push({name: 'Update', query: {rowId: row.id, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name}})
          break;
        case 'detail':
          this.$router.push({name: 'Detail', query: {rowId: row.id, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name}})
          break;
        case 'delete':
          if (this.multipleSelection.length <= 0) {
            this.$message({
              message: '请选择操作行',
              type: 'info'
            })
          } else {
            var rowIds = this.multipleSelection.map(function (item) {
              return item.id
            })
            var _this = this
            this.$confirm('是否确认删除选中的' + rowIds.length + '条记录?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function () {
              return deleteSkus({
                obj_ids: rowIds
              }).then(res => {
                if (res.data.code == 10000) {
                  _this.getDataList()
                  _this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                }
              })
            })
          }
          break;
        default:
          break;
      }
    },
    //数据请求-----------------------------------------------
    // 获取列表数据
    getDataList() {
      this.searchParams.page = this.page
      this.searchParams.size = this.size
      findSkuList(this.searchParams).then(res => {
        if (res.data.code == 10000) {
          this.totalSize = res.data.data.total_count
          this.dataList = res.data.data.list
        }
      });
    },
    findCateList() {
      findCateList().then(res => {
        if (res.data.code == 10000) {
          this.cateList = res.data.data.list
        }
      });
    },
    findActivityList() {
      findActivityList().then(res => {
        if (res.data.code == 10000) {
          this.activityList = res.data.data.list
        }
      });
    },
    updateAllStock(){
      if (this.multipleSelection.length <= 0) {
            this.$message({
              message: '请选择操作行',
              type: 'info'
            })
        } else {
          var rowIds = this.multipleSelection.map(function (item) {
            return item.id
          })
          var _this = this
          this.$confirm('是否确认修改选中的' + rowIds.length + '条记录的库存?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function () {
            return updateAllStock({
              obj_ids: rowIds,
              stock: _this.setSkuData.stock
            }).then(res => {
              if (res.data.code == 10000) {
                _this.getDataList()
                _this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          })
        }
    },
    updateAllSellingPrice(){
      if (this.multipleSelection.length <= 0) {
            this.$message({
              message: '请选择操作行',
              type: 'info'
            })
        } else {
          var rowIds = this.multipleSelection.map(function (item) {
            return item.id
          })
          var _this = this
          this.$confirm('是否确认修改选中的' + rowIds.length + '条记录的销售价?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function () {
            return updateAllSellingPrice({
              obj_ids: rowIds,
              sellingPrice: _this.setSkuData.sellingPrice
            }).then(res => {
              if (res.data.code == 10000) {
                _this.getDataList()
                _this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          })
        }
    }
  },
}
</script>


<style lang="scss" scoped></style>
